package com.ideal.frame.sys.controller;

import com.ideal.frame.common.anno.PermissionAnnotation;
import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.common.enumerate.ResponseCode;
import com.ideal.frame.common.enumerate.SecurityPermission;
import com.ideal.frame.sys.entity.SysRole;
import com.ideal.frame.sys.form.search.RoleSearchForm;
import com.ideal.frame.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色管理接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "角色列表", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("list")
    @PermissionAnnotation(SecurityPermission.sys_role_view)
    public ResponseModel getRoleList(@RequestBody RoleSearchForm roleSearchForm){
        return ResponseModel.success(sysRoleService.getRoleList(roleSearchForm));
    }

    @ApiOperation(value = "获取所有的角色列表", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getAllRoles")
    @PermissionAnnotation(SecurityPermission.sys_role_view)
    public ResponseModel getAllRoles(){
        return ResponseModel.success(sysRoleService.getAllRoles());
    }

    @ApiOperation(value = "根据id获取角色信息", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getById/{id}")
    @PermissionAnnotation(SecurityPermission.sys_role_view)
    public ResponseModel getById(@PathVariable Long id) {
        return ResponseModel.success(sysRoleService.getById(id));
    }

    @ApiOperation(value = "角色新增/修改", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("save")
    @PermissionAnnotation(SecurityPermission.sys_role_edit)
    public ResponseModel save(@RequestBody @Validated SysRole role){
        sysRoleService.save(role);
        return ResponseModel.success("保存成功");
    }

    @ApiOperation(value = "角色删除", httpMethod = "DELETE", produces = "application/json", response = ResponseModel.class)
    @DeleteMapping("del")
    @PermissionAnnotation(SecurityPermission.sys_role_del)
    public ResponseModel del(@RequestParam("ids") List<Long> ids){
        sysRoleService.delRole(ids);
        return ResponseModel.success();
    }


}
